package net.lzzy.practices.web.course;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.lzzy.practices.constants.ApiConstants;
import net.lzzy.practices.constants.Messages;
import net.lzzy.practices.entities.Applicant;
import net.lzzy.practices.entities.Course;
import net.lzzy.practices.services.ApplicantService;
import net.lzzy.practices.utils.StringUtils;
import net.lzzy.practices.web.models.ApiResult;
import net.lzzy.practices.web.models.ApplicantInfo;
import net.lzzy.practices.web.models.VmApplicant;
import net.lzzy.practices.web.models.VmCourse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author Jaymo on 2022/5/26
 */

@Api(tags = "注册相关业务")
@CrossOrigin
@RestController
@RequestMapping(ApiConstants.ROUTE_APPLICANT_ROOT)
public class ApplicantApiController {
    private  final ApplicantService service ;

    @Autowired
    public ApplicantApiController(ApplicantService service){
        this.service = service;
    }

    @ApiOperation("学生申请加入课程")
    @ApiImplicitParam(name = "info", value = "申请信息", dataType = "ApplicantInfo", dataTypeClass = ApplicantInfo.class, paramType = "body", readOnly = true)
    @PostMapping(ApiConstants.ROUTE_APPLICANT_APPLY)
    public ApiResult apply(@RequestBody ApplicantInfo info){
        String token = info.getToken();
        String strCourseId =  info.getCourseId();
        String strStudentId =  info.getStudentId();
        if (StringUtils.isEmpty(token) ||
                StringUtils.isEmpty(strCourseId) ||
                StringUtils.isEmpty(strStudentId))     {
            return  new ApiResult(false, Messages.INCOMPLETE_INFO.toString(), null);
        }
        UUID courseId = UUID.fromString(strCourseId);
        UUID studentId = UUID.fromString(strStudentId);
        Applicant applicant  =service.apply(studentId, courseId,token);
        if (applicant == null){
            String msg = Messages.No_PERMISSION + "或" + Messages.WRING_ID;
            return  new ApiResult(false,msg,null);
        }
        return  new ApiResult(true,Messages.SUCCESS.toString(), new VmApplicant(applicant));
    }
    @ApiOperation("查询已被驳回的申请课程")
    @ApiImplicitParams({@ApiImplicitParam(name = "token", value = "token", dataType = "String", dataTypeClass = String.class, paramType = "query"),
            @ApiImplicitParam(name = "token", value = "token", dataType = "String", dataTypeClass = String.class, paramType = "query")})
       @GetMapping(ApiConstants.ROUTE_APPLICANT_ENROLLED)
      public  ApiResult getEnrolledCourses(@RequestParam String token, @RequestParam(name = "id") UUID studentId){
           List<Applicant> applicants = service.getEnrolledApplicants(studentId);
       return transform(applicants);
       }
    @ApiOperation("查询正在申请的课程")
    @ApiImplicitParams({@ApiImplicitParam(name = "token", value = "token", dataType = "String", dataTypeClass = String.class, paramType = "query"),
            @ApiImplicitParam(name = "token", value = "token", dataType = "String", dataTypeClass = String.class, paramType = "query")})
       @GetMapping(ApiConstants.ROUTE_APPLICANT_APPLYING)
       public  ApiResult getApplyingCourses(@RequestParam String token, @RequestParam(name = "id") UUID studentId){
        List<Applicant> applicants = service.getApplyingApplicants(studentId);
        return  transform(applicants);
       }

       @GetMapping(ApiConstants.ROUTE_APPLICANT_DECLINED)
       @ApiOperation("查询已被驳回的申请课程")
       @ApiImplicitParams({@ApiImplicitParam(name = "token", value = "token", dataType = "String", dataTypeClass = String.class, paramType = "query"),
               @ApiImplicitParam(name = "token", value = "token", dataType = "String", dataTypeClass = String.class, paramType = "query")})
       public ApiResult getDeclinedCourses(@RequestParam String token, @RequestParam(name = "id")UUID studentId){
          List<Applicant> applicants = service.getDeclinedApplicants(studentId);
          return  transform(applicants);
       }

       private  ApiResult transform(List<Applicant> applicants){
        if (applicants == null){
            return  new ApiResult(false,Messages.WRING_ID.toString(), null);
        }
        List<VmApplicant> vmApplicants = new ArrayList<>();
        applicants.forEach(applicant ->  vmApplicants.add(new VmApplicant(applicant)));
        return  new ApiResult(true,applicants.size() +"", vmApplicants);
       }

       @PostMapping(ApiConstants.ROUTE_APPLICANT_APPROVE)
       @ApiOperation("通过申请")
       @ApiImplicitParams({@ApiImplicitParam(name = "token", value = "token", dataType = "String", dataTypeClass = String.class, paramType = "query"),
               @ApiImplicitParam(name = "token", value = "token", dataType = "String", dataTypeClass = String.class, paramType = "query")})
    public  ApiResult approve(@RequestParam String token, @RequestParam(name = "id") UUID applicantId) {
        Applicant applicant = service.approve(applicantId,token);
        if (applicant == null){
            String msg = Messages.No_PERMISSION + "或" + Messages.WRING_ID;
            return  new ApiResult(false, msg,null);
        }
        return  new ApiResult(true, Messages.SUCCESS.toString(), new VmApplicant(applicant));
       }

       @PostMapping(ApiConstants.ROUTE_APPLICANT_DECLINE)
       @ApiOperation("拒绝申请")
       @ApiImplicitParams({@ApiImplicitParam(name = "token", value = "token", dataType = "String", dataTypeClass = String.class, paramType = "query"),
               @ApiImplicitParam(name = "token", value = "token", dataType = "String", dataTypeClass = String.class, paramType = "query")})
    public  ApiResult decline(@RequestParam String token,@RequestParam(name = "id") UUID applicantId) {
        Applicant applicant = service.decline(applicantId,token);
        if (applicant ==null){
            String msg = Messages.No_PERMISSION + "或" + Messages.WRING_ID;
            return  new ApiResult(false,msg,null);
        }
        return  new ApiResult(true, Messages.SUCCESS.toString(), new VmApplicant(applicant));
       }

       @GetMapping(ApiConstants.ROUTE_APPLICANT_TEACHER_APPLYING)
       @ApiOperation("获取申请中的学生用户")
       @ApiImplicitParams({@ApiImplicitParam(name = "token", value = "token", dataType = "String", dataTypeClass = String.class, paramType = "query"),
               @ApiImplicitParam(name = "token", value = "token", dataType = "String", dataTypeClass = String.class, paramType = "query")})
    public  ApiResult getApplyingStudents(@RequestParam String token, @RequestParam(name = "id") UUID courseId){
        List<Applicant> applicants = service.getApplyingStudents(courseId);
        return  transform(applicants);
       }

       @GetMapping(ApiConstants.ROUTE_APPLICANT_TEACHER_ENROLLED)
       @ApiOperation("获取已登记的学生用户")
       @ApiImplicitParams({@ApiImplicitParam(name = "token", value = "token", dataType = "String", dataTypeClass = String.class, paramType = "query"),
               @ApiImplicitParam(name = "token", value = "token", dataType = "String", dataTypeClass = String.class, paramType = "query")})
    public  ApiResult getEnrolledStudents(@RequestParam String token, @RequestParam(name = "id") UUID courseId){
        List<Applicant> applicants = service.getEnrolledStudents(courseId);
        return  transform(applicants);
       }

       @GetMapping(ApiConstants.ROUTE_APPLICANT_TEACHER_DECLINED)
       @ApiOperation("获取已拒绝的学生用户")
       @ApiImplicitParams({@ApiImplicitParam(name = "token", value = "token", dataType = "String", dataTypeClass = String.class, paramType = "query"),
               @ApiImplicitParam(name = "token", value = "token", dataType = "String", dataTypeClass = String.class, paramType = "query")})
    public  ApiResult getDeclinedStudents(@RequestParam String token, @RequestParam(name = "id") UUID courseId){
        List<Applicant> applicants = service.getDeclinedStudents(courseId);
        return  transform(applicants);
       }

       @GetMapping(ApiConstants.ROUTE_APPLICANT_REMOVE)
       @ApiOperation("删除申请")
       @ApiImplicitParam(name = "info", value = "申请信息", dataType = "ApplicantInfo", dataTypeClass = ApplicantInfo.class, paramType = "body")
     public  ApiResult remove(@RequestBody ApplicantInfo info){
        String token = info.getToken();
        String strCourseId = info.getCourseId();
        String strStudentId = info.getStudentId();
        if (StringUtils.isEmpty(token) ||
         StringUtils.isEmpty(strCourseId) ||
         StringUtils.isEmpty(strStudentId)
         ){
            return  new ApiResult(false,Messages.INCOMPLETE_INFO.toString(), null);
        }
        UUID studentId = UUID.fromString(strStudentId);
        UUID courseId = UUID.fromString(strCourseId);
           Course course = service.removeStudent(courseId,studentId,token);
           if (course ==null){
               String msg = Messages.No_PERMISSION+ "或" + Messages.WRING_ID;
               return  new ApiResult(false,msg,null);
           }
           return new ApiResult(true,Messages.SUCCESS.toString(), VmCourse.create(course,true));
       }

}
